package com.zyh.system.JDsupermarkt.dao.impl;

import com.zyh.system.JDsupermarkt.dao.BuyCarDao;
import com.zyh.system.JDsupermarkt.domain.BuyCar;

import java.util.LinkedHashMap;
import java.util.Set;

/**
 * @Author zyh
 * @Date 2024/8/2 19:41
 * @Description BuyCarDao 实现类
 */

public class BuyCarDaoImpl implements BuyCarDao {
    public static final LinkedHashMap<Integer, BuyCar> BUY_CAR = new LinkedHashMap<>();

    /**
     * 添加商品到购物车
     *
     * @param map BuyCar 集合
     * @param car 购物车对象
     * @param id  商品id
     * @return 结束方法
     */
    @Override
    public boolean addBuyCar(LinkedHashMap<Integer, BuyCar> map, BuyCar car, Integer id) {
        map.put(id, car);
        return true;
    }

    /**
     * 展示购物车
     *
     * @param map BuyCar 集合
     */
    @Override
    public Double showBuyCay(LinkedHashMap<Integer, BuyCar> map) {
        Double money = 0.0;
        Set<Integer> keySet = map.keySet();
        for (Integer key : keySet) {
            BuyCar value = map.get(key);
            money += value.getMoney();
            System.out.println(key + "\t" + value.getGoodsName() + "\t\t\t" + value.getGoodsPrice() + "\t" + value.getBuyNum() + "\t" + value.getMoney());
        }
        System.out.println("\n总计：" + money);
        return money;
    }

    /**
     * 根据id查询指定购物车商品
     *
     * @param map BuyCar 集合
     * @param id  购物车商品集合
     * @return true
     */
    @Override
    public BuyCar selectById(LinkedHashMap<Integer, BuyCar> map, Integer id) {
        BuyCar buyCar=null;
        Set<Integer> keySet = map.keySet();
        for (Integer key : keySet) {
            if (key.equals(id)) {
                BuyCar value = map.get(key);
                buyCar=value;
                break;
            }
        }
        return buyCar;
    }

    /**
     * 根据购物车里的商品id删除对应信息
     *
     * @param map BuyCar集合
     * @param id  商品id
     * @return true
     */
    @Override
    public BuyCar deleteById(LinkedHashMap<Integer, BuyCar> map, Integer id) {
        BuyCar remove = map.remove(id);
        return remove;
    }

    /**
     * 清空购物车
     *
     * @param map BuyCar 集合
     * @return true
     */
    @Override
    public boolean clearBuyCar(LinkedHashMap<Integer, BuyCar> map) {
        map.clear();
        return true;
    }
}